package com.ligq.h265toupinpush.douyinclip

import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.view.View
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
import com.ligq.common.utils.AvFileUtils
import com.ligq.h265toupinpush.R
import com.ligq.h265toupinpush.douyinclip.MusicProcess.aacPath
import com.ligq.h265toupinpush.douyinclip.MusicProcess.dirPath
import com.ligq.h265toupinpush.douyinclip.MusicProcess.outputPath
import com.ligq.h265toupinpush.douyinclip.MusicProcess.videoPath
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import java.io.File
import java.io.IOException

class MixAvActivity : AppCompatActivity() {
    var videoView: VideoView? = null

    //    RangeSeekBar rangeSeekBar;
    lateinit var musicSeekBar: SeekBar
    lateinit var voiceSeekBar: SeekBar
    var musicVolume = 50
    var videoVolume = 50
    private val scope = MainScope()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mix_av)
        videoView = findViewById(R.id.videoView)
        //        rangeSeekBar = findViewById(R.id.rangeSeekBar);
        musicSeekBar = findViewById(R.id.musicSeekBar)
        voiceSeekBar = findViewById(R.id.voiceSeekBar)
        musicSeekBar.setMax(100)
        voiceSeekBar.setMax(100)
        musicSeekBar.progress = musicVolume
        voiceSeekBar.progress = videoVolume
        musicSeekBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                musicVolume = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {}
            override fun onStopTrackingTouch(seekBar: SeekBar) {}
        })
        voiceSeekBar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                videoVolume = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {}
            override fun onStopTrackingTouch(seekBar: SeekBar) {}
        })
    }

    lateinit var runnable: Runnable

    var duration = 0
    override fun onResume() {
        super.onResume()
        val dirFile = File(dirPath)
        if (!dirFile.exists()) {
            dirFile.mkdir()
        }
        val aacFile = File(aacPath)
        val videoFile = File(videoPath)
        if (!aacFile.exists() || !videoFile.exists()) {
            scope.launch(Dispatchers.IO) {
                try {
                    AvFileUtils.copyAssets(this@MixAvActivity, "music.mp3", aacPath)
                    AvFileUtils.copyAssets(this@MixAvActivity, "input.mp4", videoPath)
                } catch (e: IOException) {
                    e.printStackTrace()
                }
                runOnUiThread {
                    startPlay(videoPath)
                }
            }
        } else {
            startPlay(videoPath)
        }
    }

    private fun startPlay(path: String) {
        val layoutParams = videoView!!.layoutParams
        layoutParams.height = 675
        layoutParams.width = 1285
        videoView!!.layoutParams = layoutParams
        videoView!!.setVideoPath(path)
        videoView!!.start()
        videoView!!.setOnPreparedListener { mp ->
            duration = mp.duration / 1000
            mp.isLooping = true
            //                rangeSeekBar.setRange(0, duration);
//                rangeSeekBar.setValue(0, duration);
//                rangeSeekBar.setEnabled(true);
//                rangeSeekBar.requestLayout();
//                rangeSeekBar.setOnRangeChangedListener(new RangeSeekBar.OnRangeChangedListener() {
//                    @Override
//                    public void onRangeChanged(RangeSeekBar view, float min, float max, boolean isFromUser) {
//                        videoView.seekTo((int) min * 1000);
//                    }
//                });
            val handler = Handler()
            runnable =
                Runnable { //                        if (videoView.getCurrentPosition() >= rangeSeekBar.getCurrentRange()[1] * 1000) {
                    //                            videoView.seekTo((int) rangeSeekBar.getCurrentRange()[0] * 1000);
                    //                        }
                    handler.postDelayed(runnable, 1000)
                }
            handler.postDelayed(runnable, 1000)
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        scope.cancel()
    }

    fun music(view: View) {
        val startTimeUs = 100 * 1000 * 1000L
        val endTimeUs = 120 * 1000 * 1000L
        scope.launch(Dispatchers.IO) {
            MusicProcess.mixAudioTrack(
                aacPath,
                videoPath,
                outputPath,
                startTimeUs,
                endTimeUs,
                musicVolume,
                videoVolume
            )
        }

    }
}